/*!
 * Bookmarklet that generates a QR-Code + link of the location your browsing on
 * Google Maps to redirect you to Topheman Street View on this same location
 * 
 * http://streetview.topheman.com/
 *
 * Copyright 2013-2014, Christophe Rosset (Topheman)
 * http://labs.topheman.com/
 * http://twitter.com/topheman
 * 
 * Bookmartlet script
 * 
 * Inspired by https://code.google.com/p/qrbookmarklet/
 * 
 * Called from a bookmarklet, like :
 * 
 * javascript:(function(){if(document.getElementById){var x=document.body;var o=document.createElement('script');if(typeof(o)!='object') o=document.standardCreateElement('script');o.setAttribute('src','https://topheman-bookmarklets.googlecode.com/git/topheman-street-view.js');o.setAttribute('type','text/javascript');x.appendChild(o);}})();
 * 
 */

(function(document){

    var getLink,
//        testRegExp = /^http.+?\/@[0-9\.]+,[0-9\.]+.*?$/g,
        matchRegExp = /(?:\/@([0-9]+\.[0-9]+)(?=(?:,)([0-9]+\.[0-9]+)))/,
        coordsTest = window.location && window.location.href ? window.location.href.match(matchRegExp) : null;
    
    if(typeof gApplication !== "undefined" && gApplication.getMap){
        getLink = function() {
            return 'http://streetview.topheman.com/#lat='+gApplication.getMap().getCenter().lat()+'&lon='+gApplication.getMap().getCenter().lng();
        };
    }
    else if(typeof google !== "undefined" && google.nav){
        getLink = function() {
            var coords = google.nav.getLocation().split('@')[1].split(',');
            return 'http://streetview.topheman.com/#lat='+coords[0]+'&lon='+coords[1];
        };
    }
    else if(coordsTest !== null && coordsTest.length > 0){
        getLink = function() {
            return 'http://streetview.topheman.com/#lat='+coordsTest[1]+'&lon='+coordsTest[2];
        };
    }
    else{
        alert("You must be on Google Maps to use this bookmarklet");
        return false;
    }

    function cleanUp(e) {
        var node = document.getElementsByTagName("body")[0];
        var a = [];
        var re = new RegExp('\\bshaunsational_qr\\b');
        var els = node.getElementsByTagName("*");
        for (var i = 0, j = els.length; i < j; i++) {
            if (re.test(els[i].className)){
                a.push(els[i]);
            }
        }

        for (var x in a) {
            if (a[x].parentNode) {
                a[x].parentNode.removeChild(a[x]);
            }
        }
        document.body.removeEventListener('click', cleanUp, false);
    }

    cleanUp();

    var link = getLink();
    
    //encode url before chart processing
    var qrData = link.replace('/#lat=','%2F%23lat%3D');
    var qrData = qrData.replace('&lon=','%26lon%3D');
    qrData = qrData.replace('://','%3A%2F%2F');

    var shade = document.createElement("div");
    shade.setAttribute('class', 'shaunsational_qr');
    shade.setAttribute('id', 'shade');
    shade.setAttribute('style', 'background-color:#000;height:100%;left:0;opacity:0.65;position:fixed;top:0;width:100%;z-index:1001;');

    var centr = document.createElement("div");
    centr.setAttribute('class', 'shaunsational_qr');
    centr.setAttribute('id', 'centr');
    centr.setAttribute('style', 'background-color:transparent;height:100%;left:0;position:fixed;text-align:center;top:0;width:100%;z-index:1001;');

    var qrdiv = document.createElement("div");
    qrdiv.setAttribute('class', 'shaunsational_qr');
    qrdiv.setAttribute('id', 'qrdiv');
    qrdiv.setAttribute('style', 'margin:25px auto auto auto;background-color:#FFF;height:320px;width:320px;z-index:1001;-moz-border-radius:20px;');

    var qrcode = document.createElement("img");
    qrcode.setAttribute('class', 'shaunsational_qr');
    qrcode.setAttribute('id', 'qrcode');
    qrcode.setAttribute('style', 'margin:10px;cursor:pointer;');
    qrcode.setAttribute('src', 'https://chart.googleapis.com/chart?cht=qr&chs=300x300&chld=H|0&chl=' + qrData);
    qrcode.setAttribute('title', 'Click to close.');

    var qrurl = document.createElement("div");
    qrurl.setAttribute('class', 'shaunsational_qr');
    qrurl.setAttribute('id', 'qrurl');
    qrurl.setAttribute('style', 'background:#FFF;margin:5px auto auto auto;padding:2px 15px;cursor:pointer;width:290px;font-size:10px;color:#333;-moz-border-radius:20px;text-align:left;overflow:hidden; text-align:center;');
    
    var qranchor = document.createElement("a");
    qranchor.setAttribute('class', 'shaunsational_qr');
    qranchor.setAttribute('id', 'qranchor');
    qranchor.setAttribute('href', link);
    qranchor.title = "Click to access the panorama on Topheman Street View";
    
    var qrurl_text = document.createTextNode(link);
    qranchor.appendChild(qrurl_text);
    
    qrurl.appendChild(qranchor);


    centr.appendChild(qrdiv);
    centr.appendChild(qrurl);
    qrdiv.appendChild(qrcode);
    document.body.appendChild(shade);
    document.body.appendChild(centr);
    document.body.addEventListener('click', cleanUp, false);

})(document);